# task
# 实现shell sed 命令的替换功能
# 在命令行接收两个参数，用参数二替换掉参数一
# 如果只接收到一个参数，则打印参数所在行

import sys

def func_sed(*args):
    if len(args) == 2:
        with open(args[1], 'r', encoding='utf-8') as f:
            for line in f:
                if args[0] in line:
                    print(line)
    elif len(args) == 3:
        f1 = open(args[2], 'r', encoding='utf-8')
        f2 = open(args[2] + '.swap', 'w')
        for line in f1:
            if args[0] in line:
                line = line.replace(args[0], args[1])
            f2.write(line)

        f1.close()
        f2.close()
    else:
        print('parameter error')
        print(sys.argv[0] + ' str1 str2 filename')

func_sed(*sys.argv[1:])